package com.myzf.ncov.service.impl;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.myzf.ncov.entity.AreaData;
import com.myzf.ncov.entity.CityData;
import com.myzf.ncov.entity.ResultSet;
import com.myzf.ncov.service.abstracts.DataHandleAbstract;
import com.myzf.ncov.utils.MapCacheUtil;
import com.myzf.ncov.utils.WeChatUtil;
import lombok.extern.slf4j.Slf4j;
import me.xuxiaoxiao.chatapi.wechat.entity.contact.WXContact;
import me.xuxiaoxiao.chatapi.wechat.entity.contact.WXGroup;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 数据处理service实现类
 *
 * @author limuyi
 * @version V1.0
 * Created by limuyi on 2020-02-01 14:57
 */
@Slf4j
@Service
public class SituationDataHandleServiceImpl extends DataHandleAbstract {
    private static String key = "";

    @Override
    protected void sendMsg(String result) {


        if(StringUtils.isEmpty(key)){
            HashMap<String, WXGroup> stringWXGroupHashMap = WeChatUtil.getWeChatClient().userGroups();
            stringWXGroupHashMap.forEach((k,v)->{
                System.out.println(v.namePY);
                if(v.namePY.equals("HOME")){
                    key = k;
                }
            });
        }
        WXContact contact = WeChatUtil.getWeChatClient().userContact(key);
        if(contact !=null){
            WeChatUtil.getWeChatClient().sendText(contact,result);

        }

    }

    @Override
    public String realDeal(String data) {
        try {
            log.info(this.getClass().getSimpleName()+"开始处理数据");
            JsonObject returnObj = new JsonParser().parse(data).getAsJsonObject();
            if (null != returnObj.get("data").getAsJsonObject()) {
                JsonObject jsonObject = returnObj.get("data").getAsJsonObject();

                Gson gson = new Gson();
                ResultSet resultSet = gson.fromJson(jsonObject, ResultSet.class);

                JsonArray asJsonArray = returnObj.get("data").getAsJsonObject().get("area").getAsJsonArray();

                ArrayList<AreaData> list = new Gson().fromJson(asJsonArray, new TypeToken<ArrayList<AreaData>>() {
                }.getType());
                String result = "";
                MapCacheUtil map = MapCacheUtil.getInstance();

                for (AreaData p : list) {
                    assert p != null;
                    String pd = String.format("\n"+p.getProvinceShortName()+"\n确诊：%s，疑似：%s，治愈：%s，死亡：%s\n",
                            p.getConfirmedCount(), p.getSuspectedCount(), p.getCuredCount(), p.getDeadCount());

                    assert p.getCities() != null;
                    String s = "";
                    for(int i=1;i<=p.getCities().size();i++){
                        CityData city = p.getCities().get(i - 1);
                        assert  city != null;
                        String yc = String.format("\n排名%s："+city.getCityName()+"\n确诊：%s，疑似：%s，治愈：%s，死亡：%s",
                                i,city.getConfirmedCount(), city.getSuspectedCount(), city.getCuredCount(), city.getDeadCount());
                        if(p.getCities().size() == 1){
                            yc = yc.substring(5,yc.length());
                        }
                        if(map.getCacheItem(p.getProvinceName()+city.getCityName()) == null){
                            map.putCacheItem(p.getProvinceName()+city.getCityName(),yc);
                            s += yc;
                        }else {
                            String value = String.valueOf(map.getCacheItem(p.getProvinceName()+city.getCityName()));
                            if (null != value && !value.equals(yc)) {
                                map.putCacheItem(p.getProvinceName()+city.getCityName(),yc);
                                s += yc;
                            }
                        }
                    }
                    if(map.getCacheItem(p.getProvinceName()) == null){
                        map.putCacheItem(p.getProvinceName(),pd);
                        result += "\n------------------------"+pd + s;
                    }else {
                        String value = String.valueOf(map.getCacheItem(p.getProvinceName()));
                        if (null != value && !value.equals(pd)) {
                            map.putCacheItem(p.getProvinceName(),pd);
                            result += "\n------------------------"+pd + s;
                        }
                    }

                }
                String nationwide = String.format("全国\n确诊：%s，疑似：%s，治愈：%s，死亡：%s\n",
                         resultSet.getDiagnosed(), resultSet.getSuspect(), resultSet.getCured(), resultSet.getDeath());
                if(map.getCacheItem("china") == null){
                    map.putCacheItem("china",nationwide);
                    result = nationwide + result;
                }else {
                    String value = String.valueOf(map.getCacheItem("china"));
                    if (null != value && !value.equals(nationwide)) {
                        map.putCacheItem("china",nationwide);
                        result = nationwide + result;
                    }
                }
                return result == ""?result:String.format("时间：%s\n\n%s",resultSet.getDate(),result);
            }
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
        }finally {
            log.info(this.getClass().getSimpleName()+"完成处理数据");

        }

        return null;
    }

    public static void main(String[] args) {
        Long.valueOf("");
    }


}
